home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / ghostscript / src / ztype.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  9KB  |  352 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* ztype.c */
  20. /* Type, attribute, and conversion operators for Ghostscript */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "stream.h"
  26. #include "errors.h"
  27. #include "oper.h"
  28. #include "dict.h"
  29. #include "iname.h"
  30. #include "iscan.h"
  31. #include "iutil.h"
  32. #include "store.h"
  33.  
  34. /* Forward references */
  35. private int near access_check(P3(os_ptr, int, int));
  36. private int convert_to_string(P2(os_ptr, os_ptr));
  37.  
  38. /*
  39.  * Max and min integer values expressed as reals.
  40.  * Note that these are biased by 1 to allow for truncation.
  41.  * They should be #defines rather than static consts, but
  42.  * several of the SCO Unix compilers apparently can't handle this.
  43.  * On the other hand, the DEC compiler can't handle casts in
  44.  * constant expressions, so we can't use min_long and max_long.
  45.  * What a nuisance!
  46.  */
  47. #define alt_min_long (-1L << (arch_sizeof_long * 8 - 1))
  48. #define alt_max_long (~(alt_min_long))
  49. private const double min_int_real = (alt_min_long * 1.0 - 1);
  50. private const double max_int_real = (alt_max_long * 1.0 + 1);
  51. #define real_can_be_int(v)\
  52.   ((v) > min_int_real && (v) < max_int_real)
  53. int
  54. zcvi_possible(floatp realval)
  55. {    return real_can_be_int(realval);
  56. }
  57.  
  58. /* Get the pointer to the access flags for a ref. */
  59. #define access_ref(opp)\
  60.   (r_has_type(opp, t_dictionary) ? dict_access_ref(opp) : opp)
  61.  
  62. /* Initialize the table of type names. */
  63. /* We export the type names just in case they might be useful. */
  64. ref type_names[t_next_index];
  65. private void
  66. ztype_init(void)
  67. {    static const char *tnames[] = { type_name_strings };
  68.     int i;
  69.     for ( i = 0; i < t_next_index; i++ )
  70.        {    name_enter(tnames[i], &type_names[i]);
  71.         r_set_attrs(&type_names[i], a_executable);
  72.        }
  73. }
  74.  
  75. /* <obj> type <name> */
  76. int
  77. ztype(register os_ptr op)
  78. {    ref *ptref;
  79.     check_op(1);
  80.     ptref = &type_names[r_btype(op)];
  81.     ref_assign(op, ptref);
  82.     return 0;
  83. }
  84.  
  85. /* <obj> cvlit <obj> */
  86. int
  87. zcvlit(register os_ptr op)
  88. {    ref *aop;
  89.     check_op(1);
  90.     aop = access_ref(op);
  91.     r_clear_attrs(aop, a_executable);
  92.     return 0;
  93. }
  94.  
  95. /* <obj> cvx <obj> */
  96. int
  97. zcvx(register os_ptr op)
  98. {    ref *aop;
  99.     check_op(1);
  100.     aop = access_ref(op);
  101.     r_set_attrs(aop, a_executable);
  102.     return 0;
  103. }
  104.  
  105. /* <obj> xcheck <bool> */
  106. int
  107. zxcheck(register os_ptr op)
  108. {    check_op(1);
  109.     make_bool(op, (r_has_attr(access_ref(op), a_executable) ? 1 : 0));
  110.     return 0;
  111. }
  112.  
  113. /* <obj:array|packedarray|file|string> executeonly <obj> */
  114. int
  115. zexecuteonly(register os_ptr op)
  116. {    check_op(1);
  117.     if ( r_has_type(op, t_dictionary) )
  118.         return_error(e_typecheck);
  119.     return access_check(op, a_execute, 1);
  120. }
  121.  
  122. /* <obj:array|packedarray|dict|file|string> noaccess <obj> */
  123. int
  124. znoaccess(register os_ptr op)
  125. {    return access_check(op, 0, 1);
  126. }
  127.  
  128. /* <obj:array|packedarray|dict|file|string> readonly <obj> */
  129. int
  130. zreadonly(register os_ptr op)
  131. {    return access_check(op, a_readonly, 1);
  132. }
  133.  
  134. /* <array|packedarray|dict|file|string> rcheck <bool> */
  135. int
  136. zrcheck(register os_ptr op)
  137. {    int code = access_check(op, a_read, 0);
  138.     if ( code >= 0 ) make_bool(op, code), code = 0;
  139.     return code;
  140. }
  141.  
  142. /* <array|packedarray|dict|file|string> wcheck <bool> */
  143. int
  144. zwcheck(register os_ptr op)
  145. {    int code = access_check(op, a_write, 0);
  146.     if ( code >= 0 ) make_bool(op, code), code = 0;
  147.     return code;
  148. }
  149.  
  150. /* <num> cvi <int> */
  151. /* <string> cvi <int> */
  152. int
  153. zcvi(register os_ptr op)
  154. {    float fval;
  155.     switch ( r_type(op) )
  156.        {
  157.     case t_integer:
  158.         return 0;
  159.     case t_real:
  160.         fval = op->value.realval;
  161.         break;
  162.     default:
  163.         return_error(e_typecheck);
  164.     case t_string:
  165.        {    ref nref;
  166.         int code;
  167.         code = scan_number_only(op, &nref);
  168.         if ( code )        /* error condition */
  169.             return code;
  170.         if ( r_has_type(&nref, t_integer) )
  171.         {    *op = nref;
  172.             return 0;
  173.         }
  174.         /* Otherwise, result was a real */
  175.         fval = nref.value.realval;
  176.        }
  177.        }
  178.     /* Check if a real will fit into an integer value */
  179.     if ( !zcvi_possible(fval) )
  180.         return_error(e_rangecheck);
  181.     make_int(op, (long)fval);    /* truncates towards 0 */
  182.     return 0;
  183. }
  184.  
  185. /* <string> cvn <name> */
  186. int
  187. zcvn(register os_ptr op)
  188. {    check_read_type(*op, t_string);
  189.     return name_from_string(op, op);
  190. }
  191.  
  192. /* <num> cvr <real> */
  193. /* <string> cvr <real> */
  194. int
  195. zcvr(register os_ptr op)
  196. {    switch ( r_type(op) )
  197.        {
  198.     case t_integer:
  199.         make_real(op, op->value.intval);
  200.     case t_real:
  201.         return 0;
  202.     default:
  203.         return_error(e_typecheck);
  204.     case t_string:
  205.        {    ref nref;
  206.         int code;
  207.         code = scan_number_only(op, &nref);
  208.         if ( code ) return code;    /* error condition */
  209.         if ( r_has_type(&nref, t_real) ) { *op = nref; return 0; }
  210.         /* Otherwise, result was an integer */
  211.         make_real(op, nref.value.intval);
  212.         return 0;
  213.        }
  214.        }
  215. }
  216.  
  217. /* <num> <radix_int> <string> cvrs <substring> */
  218. int
  219. zcvrs(register os_ptr op)
  220. {    int radix;
  221.     check_type(op[-1], t_integer);
  222.     if ( op[-1].value.intval < 2 || op[-1].value.intval > 36 )
  223.         return_error(e_rangecheck);
  224.     radix = op[-1].value.intval;
  225.     check_write_type(*op, t_string);
  226.     if ( radix == 10 )
  227.        {    switch ( r_type(op - 2) )
  228.            {
  229.         case t_integer: case t_real:
  230.            {    int code = convert_to_string(op - 2, op);
  231.             if ( code < 0 ) return code;
  232.             pop(2);
  233.             return 0;
  234.            }
  235.         default:
  236.             return_error(e_typecheck);
  237.            }
  238.        }
  239.     else
  240.        {    ulong ival;
  241.         byte digits[32];
  242.         byte *endp = &digits[32];
  243.         byte *dp = endp;
  244.         switch ( r_type(op - 2) )
  245.            {
  246.         case t_integer:
  247.             ival = (ulong)op[-2].value.intval;
  248.             break;
  249.         case t_real:
  250.            {    float fval = op[-2].value.realval;
  251.             if ( !real_can_be_int(fval) )
  252.                 return_error(e_rangecheck);
  253.             ival = (ulong)(long)fval;
  254.            }    break;
  255.         default:
  256.             return_error(e_typecheck);
  257.            }
  258.         do
  259.            {    int dit = ival % radix;
  260.             *--dp = dit + (dit < 10 ? '0' : ('A' - 10));
  261.             ival /= radix;
  262.            }
  263.         while ( ival );
  264.         if ( endp - dp > r_size(op) ) return_error(e_rangecheck);
  265.         memcpy(op->value.bytes, dp, (uint)(endp - dp));
  266.         r_set_size(op, endp - dp);
  267.        }
  268.     op[-2] = *op;
  269.     pop(2);
  270.     return 0;
  271. }
  272.  
  273. /* <any> <string> cvs <substring> */
  274. int
  275. zcvs(register os_ptr op)
  276. {    int code;
  277.     check_write_type(*op, t_string);
  278.     code = convert_to_string(op - 1, op);
  279.     if ( code >= 0 ) pop(1);
  280.     return code;
  281. }
  282.  
  283. /* ------ Initialization procedure ------ */
  284.  
  285. op_def ztype_op_defs[] = {
  286.     {"1cvi", zcvi},
  287.     {"1cvlit", zcvlit},
  288.     {"1cvn", zcvn},
  289.     {"1cvr", zcvr},
  290.     {"3cvrs", zcvrs},
  291.     {"2cvs", zcvs},
  292.     {"1cvx", zcvx},
  293.     {"1executeonly", zexecuteonly},
  294.     {"1noaccess", znoaccess},
  295.     {"1rcheck", zrcheck},
  296.     {"1readonly", zreadonly},
  297.     {"1type", ztype},
  298.     {"1wcheck", zwcheck},
  299.     {"1xcheck", zxcheck},
  300.     op_def_end(ztype_init)
  301. };
  302.  
  303. /* ------ Internal routines ------ */
  304.  
  305. /* Test or modify the access of an object. */
  306. /* If modify = 1, restrict to the selected access and return 0; */
  307. /* if modify = 0, do not change the access, and return 1 */
  308. /* if the object had the access. */
  309. /* Return an error code if the object is not of appropriate type, */
  310. /* or if the object did not have the access already when modify=1. */
  311. private int near
  312. access_check(os_ptr op,
  313.     int access,                /* mask for attrs */
  314.     int modify)                /* if true, reduce access */
  315. {    ref *aop = op;
  316.     switch ( r_type(op) )
  317.        {
  318.     default:
  319.         return_error(e_typecheck);
  320.     case t_dictionary:
  321.         aop = dict_access_ref(op);
  322.     case t_array: case t_file: case t_gstate: case t_string:
  323.     case t_mixedarray: case t_shortarray: ;
  324.        }
  325.     if ( modify )
  326.        {    if ( !r_has_attrs(aop, access) )
  327.             return_error(e_invalidaccess);
  328.         if ( aop != op )    /* i.e., t_dictionary */
  329.            {    ref_save(aop, "access_check(modify)");
  330.            }
  331.         r_clear_attrs(aop, a_all);
  332.         r_set_attrs(aop, access);
  333.         return 0;
  334.        }
  335.     else
  336.        {    return (r_has_attrs(aop, access)? 1 : 0);
  337.        }
  338. }
  339.  
  340. /* Do all the work of cvs.  The destination has been checked, but not */
  341. /* the source.  This is a separate procedure so that */
  342. /* cvrs can use it when the radix is 10. */
  343. private int
  344. convert_to_string(os_ptr op1, os_ptr op)
  345. {    uint len;
  346.     int code = obj_cvs(op1, op->value.bytes, r_size(op), &len);
  347.     if ( code < 0 ) return code;
  348.     *op1 = *op;
  349.     r_set_size(op1, len);
  350.     return 0;
  351. }
  352.